package jsp;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author MissChen
 * @date 2024年05月14日 16:46
 */

@WebServlet("*.session")
public class SessionServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String path=request.getServletPath();
        switch (path){
            case "/add.session":
                addSession(request,response);
                break;
            case "/find.session":
                findSession(request,response);
                break;
            case "/modify.session":
                modifySession(request,response);
                break;
            case "/remove.session":
                removeSession(request,response);
                break;
            case "/clear.session":
                clearSession(request,response);
                break;
            default:
                PrintWriter out=response.getWriter();
                out.println("<h1>您输入的请求尚未处理，请检查一下是否正确</h1>");
        }
    }
    public void addSession(HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();
        session.setAttribute("msg","Hello Session");
        Emp emp=new Emp();
        emp.setId(1);
        emp.setName("花花");
        emp.setSalary(7000.0);
        session.setAttribute("emp",emp);
    }
    public void findSession(HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();
        String msg= (String) session.getAttribute("msg");
        System.out.println(msg);
        Emp emp= (Emp) session.getAttribute("emp");
        System.out.println(emp);
        // System.out.println(emp.getId()+"---"+emp.getName()+"---"+emp.getSalary());
    }
    public void modifySession(HttpServletRequest request, HttpServletResponse response){
        //修改本质上就是添加，只要key相同则覆盖
        HttpSession session=request.getSession();
        // 设置session的生存时间，单位:秒
        // 默认情况下，session的生存时间是30分钟
        //正式开发中，一般比建议修改生存时间
        // session.setMaxInactiveInterval(3600);
        session.setAttribute("msg","Hello World");
        Emp emp=new Emp();
        emp.setId(2);
        emp.setName("刘亦菲");
        emp.setSalary(10000.0);
        session.setAttribute("emp",emp);


    }

    public void removeSession(HttpServletRequest request, HttpServletResponse response){

        //方式一:删除指定key的数据
        request.getSession().removeAttribute("msg");
    }
    public void clearSession(HttpServletRequest request, HttpServletResponse response){

        //方式二:清空所有session
        // 本质上不是情况，而是销毁原有的session对象,重新创建了一个新的对象
        request.getSession().invalidate();
    }

}
